package com.eyun.system.service;

import cn.hutool.core.bean.BeanUtil;
import com.eyun.system.domain.DataPermissionRule;
import com.eyun.system.repository.DataPermissionRuleRepository;
import com.eyun.system.service.dto.DataPermissionRuleDTO;
import com.eyun.system.service.mapper.DataPermissionRuleMapper;
import java.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.CacheManager;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

// jhipster-needle-add-import - JHipster will add getters and setters here, do not remove

/**
 * Service Implementation for managing {@link DataPermissionRule}.
 */
@Service
@Transactional
public class DataPermissionRuleService {

    private final Logger log = LoggerFactory.getLogger(DataPermissionRuleService.class);

    private final DataPermissionRuleRepository dataPermissionRuleRepository;

    private final CacheManager cacheManager;

    private final DataPermissionRuleMapper dataPermissionRuleMapper;

    public DataPermissionRuleService(
        DataPermissionRuleRepository dataPermissionRuleRepository,
        CacheManager cacheManager,
        DataPermissionRuleMapper dataPermissionRuleMapper
    ) {
        this.dataPermissionRuleRepository = dataPermissionRuleRepository;
        this.cacheManager = cacheManager;
        this.dataPermissionRuleMapper = dataPermissionRuleMapper;
    }

    /**
     * Save a dataPermissionRule.
     *
     * @param dataPermissionRuleDTO the entity to save.
     * @return the persisted entity.
     */
    public DataPermissionRuleDTO save(DataPermissionRuleDTO dataPermissionRuleDTO) {
        log.debug("Request to save DataPermissionRule : {}", dataPermissionRuleDTO);
        DataPermissionRule dataPermissionRule = dataPermissionRuleMapper.toEntity(dataPermissionRuleDTO);
        dataPermissionRule = dataPermissionRuleRepository.save(dataPermissionRule);
        return dataPermissionRuleMapper.toDto(dataPermissionRule);
    }

    /**
     * Partially update a dataPermissionRule.
     *
     * @param dataPermissionRuleDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<DataPermissionRuleDTO> partialUpdate(DataPermissionRuleDTO dataPermissionRuleDTO) {
        log.debug("Request to partially update DataPermissionRule : {}", dataPermissionRuleDTO);

        return dataPermissionRuleRepository
            .findById(dataPermissionRuleDTO.getId())
            .map(
                existingDataPermissionRule -> {
                    dataPermissionRuleMapper.partialUpdate(existingDataPermissionRule, dataPermissionRuleDTO);

                    return existingDataPermissionRule;
                }
            )
            .map(dataPermissionRuleRepository::save)
            .map(dataPermissionRuleMapper::toDto);
    }

    /**
     * Get all the dataPermissionRules.
     *
     * @param pageable the pagination information.
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public Page<DataPermissionRuleDTO> findAll(Pageable pageable) {
        log.debug("Request to get all DataPermissionRules");
        return dataPermissionRuleRepository.findAll(pageable).map(dataPermissionRuleMapper::toDto);
    }

    /**
     * count all the dataPermissionRules.
     * @return the count of entities
     */
    @Transactional(readOnly = true)
    public long count() {
        log.debug("Request to count all DataPermissionRules");
        return dataPermissionRuleRepository.count();
    }

    /**
     * Get one dataPermissionRule by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<DataPermissionRuleDTO> findOne(Long id) {
        log.debug("Request to get DataPermissionRule : {}", id);
        return dataPermissionRuleRepository.findById(id).map(dataPermissionRuleMapper::toDto);
    }

    /**
     * Get one dataPermissionRule by example.
     *
     * @param example the example of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<DataPermissionRuleDTO> findOneByExample(Example<DataPermissionRule> example) {
        log.debug("Request to get DataPermissionRule by example");
        return dataPermissionRuleRepository.findOne(example).map(dataPermissionRuleMapper::toDto);
    }

    /**
     * Get all the dataPermissionRules by example.
     *
     * @param example the example of the entity.
     * @return the entities.
     */
    @Transactional(readOnly = true)
    public Page<DataPermissionRuleDTO> findAllByExample(Example<DataPermissionRule> example, Pageable pageable) {
        log.debug("Request to get DataPermissionRule by example");
        return dataPermissionRuleRepository.findAll(example, pageable).map(dataPermissionRuleMapper::toDto);
    }

    /**
     * Delete the dataPermissionRule by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete DataPermissionRule : {}", id);
        dataPermissionRuleRepository.deleteById(id);
    }

    /**
     * Update ignore specified fields by dataPermissionRule
     */
    public DataPermissionRuleDTO updateByIgnoreSpecifiedFields(
        DataPermissionRuleDTO changeDataPermissionRuleDTO,
        Set<String> unchangedFields
    ) {
        DataPermissionRuleDTO dataPermissionRuleDTO = findOne(changeDataPermissionRuleDTO.getId()).get();
        BeanUtil.copyProperties(changeDataPermissionRuleDTO, dataPermissionRuleDTO, unchangedFields.toArray(new String[0]));
        dataPermissionRuleDTO = save(dataPermissionRuleDTO);
        return dataPermissionRuleDTO;
    }

    /**
     * Update specified fields by dataPermissionRule
     */
    public DataPermissionRuleDTO updateBySpecifiedFields(DataPermissionRuleDTO changeDataPermissionRuleDTO, Set<String> fieldNames) {
        DataPermissionRuleDTO dataPermissionRuleDTO = findOne(changeDataPermissionRuleDTO.getId()).get();
        DataPermissionRuleDTO finalDataPermissionRuleDTO = dataPermissionRuleDTO;
        fieldNames.forEach(
            fieldName -> {
                BeanUtil.setFieldValue(
                    finalDataPermissionRuleDTO,
                    fieldName,
                    BeanUtil.getFieldValue(changeDataPermissionRuleDTO, fieldName)
                );
            }
        );
        dataPermissionRuleDTO = save(finalDataPermissionRuleDTO);
        return dataPermissionRuleDTO;
    }

    /**
     * Update specified field by dataPermissionRule
     */
    public DataPermissionRuleDTO updateBySpecifiedField(DataPermissionRuleDTO changeDataPermissionRuleDTO, String fieldName) {
        DataPermissionRuleDTO dataPermissionRuleDTO = findOne(changeDataPermissionRuleDTO.getId()).get();
        BeanUtil.setFieldValue(dataPermissionRuleDTO, fieldName, BeanUtil.getFieldValue(changeDataPermissionRuleDTO, fieldName));
        dataPermissionRuleDTO = save(dataPermissionRuleDTO);
        return dataPermissionRuleDTO;
    }
    // jhipster-needle-service-add-method - JHipster will add getters and setters here, do not remove

}
